from common.config import PathConfig
from os.path import exists


class CacheControl:
    def __init__(self, cache_name):
        self.cache_path = PathConfig.cache_path
        self.cache_name = cache_name

    @property
    def cache(self) -> str:
        if exists(self.cache_path+self.cache_name):
            with open(self.cache_path+self.cache_name, 'r', encoding="utf-8") as f:
                value = f.read()
            return value
        else:
            raise NameError("缓存文件不存在")

    @cache.setter
    def cache(self, value: str):
        if isinstance(value, str):
            with open(self.cache_path+self.cache_name, 'w', encoding="utf-8") as f:
                f.write(value)
        else:
            raise TypeError("请输入字符串类型")


if __name__ == '__main__':
    c = CacheControl('test1')
    c.cache = "abcf"
    print(c.cache)